package com.iraeta.service.etl;

import com.iraeta.dao.bpm.CaiWuDao;
import com.iraeta.dao.bpm.SalDao;
import com.iraeta.dao.main.*;
import com.iraeta.entity.main.*;
import com.iraeta.uitl.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class BpmEtlService {
    @Autowired
    BpmSalxshtmDao bpmSalxshtmDao;
    @Autowired
    BpmSalxshtcDao bpmSalxshtcDao;
    @Autowired
    BpmSalxsddmDao bpmSalxsddmDao;
    @Autowired
    BpmSalxsddcDao bpmSalxsddcDao;
    @Autowired
    SalDao salDao;

    @Autowired
    BpmErClbxdmDao bpmErClbxdmDao;
    @Autowired
    BpmErRcfybxdmDao bpmErRcfybxdmDao;
    @Autowired
    BpmPurFksqdmDao bpmPurFksqdmDao;
    @Autowired
    CaiWuDao caiWuDao;

    @Autowired
    EtllogDao etllogDao;

    public void processBpmSalXshtm(){
        List<HashMap<String, Object>> res = salDao.listBpmSalxshtm();
        int insertCount = 0;
        int updateCount = 0;
        int exceptionCount = 0;
        for (HashMap<String, Object> map : res) {
            BpmSalxshtm item = new BpmSalxshtm();
            Util.mapToBean(map, item);
            BpmSalxshtm bpmSalxshtmDb = bpmSalxshtmDao.selectByPrimaryKey(item.getIid());
            if(bpmSalxshtmDb == null){
                bpmSalxshtmDao.insert(item);
                insertCount++;
            }else{
                if(!item.equals(bpmSalxshtmDb)){
                    bpmSalxshtmDao.updateByPrimaryKey(item);
                    updateCount++;
                }
            }
        }
        Etllog etllog = new Etllog("bpm销售合同", insertCount, updateCount, exceptionCount, 0);
        etllogDao.insert(etllog);
        System.out.println("bpm销售合同新增" + insertCount + "条,修改" + updateCount + "条,异常处理" + exceptionCount + "条");
    }

    public void processBpmSalXshtc(){
        List<HashMap<String, Object>> res = salDao.listBpmSalxshtc();
        int insertCount = 0;
        int updateCount = 0;
        int exceptionCount = 0;
        for (HashMap<String, Object> map : res) {
            try {
                BpmSalxshtc item = new BpmSalxshtc();
                Util.mapToBean(map, item);
                BpmSalxshtc bpmSalxshtcDb = bpmSalxshtcDao.selectByPrimaryKey(item.getIautoid());
                if(bpmSalxshtcDb == null){
                    bpmSalxshtcDao.insert(item);
                    insertCount++;
                }else{
                    if(!item.equals(bpmSalxshtcDb)){
                        bpmSalxshtcDao.updateByPrimaryKey(item);
                        updateCount++;
                    }
                }
            }catch (Exception e){
                exceptionCount++;
            }
        }
        Etllog etllog = new Etllog("bpm销售合同明细", insertCount, updateCount, exceptionCount, 0);
        etllogDao.insert(etllog);
        System.out.println("bpm销售合同明细新增" + insertCount + "条,修改" + updateCount + "条,异常处理" + exceptionCount + "条");
    }

    public void processBpmSalXsddm(){
        List<HashMap<String, Object>> res = salDao.listBpmSalxsddm();
        int insertCount = 0;
        int updateCount = 0;
        int exceptionCount = 0;
        for (HashMap<String, Object> map : res) {
            try {
                BpmSalxsddm item = new BpmSalxsddm();
                Util.mapToBean(map, item);
                BpmSalxsddm bpmSalxsddmDb = bpmSalxsddmDao.selectByPrimaryKey(item.getIid());
                if(bpmSalxsddmDb == null){
                    bpmSalxsddmDao.insert(item);
                    insertCount++;
                }else{
                    if(!item.equals(bpmSalxsddmDb)){
                        bpmSalxsddmDao.updateByPrimaryKey(item);
                        updateCount++;
                    }
                }
            }catch (Exception e){
                exceptionCount++;
            }
        }
        Etllog etllog = new Etllog("bpm销售订单", insertCount, updateCount, exceptionCount, 0);
        etllogDao.insert(etllog);
        System.out.println("bpm销售订单新增" + insertCount + "条,修改" + updateCount + "条,异常处理" + exceptionCount + "条");
    }

    public void processBpmSalXsddc(){
        List<HashMap<String, Object>> res = salDao.listBpmSalxsddc();
        int insertCount = 0;
        int updateCount = 0;
        int exceptionCount = 0;
        for (HashMap<String, Object> map : res) {
            try {
                BpmSalxsddcWithBLOBs item = new BpmSalxsddcWithBLOBs();
                Util.mapToBean(map, item);
                BpmSalxsddcWithBLOBs bpmSalxsddcWithBLOBsDb = bpmSalxsddcDao.selectByPrimaryKey(item.getIautoid());
                if(bpmSalxsddcWithBLOBsDb == null){
                    bpmSalxsddcDao.insert(item);
                    insertCount++;
                }else{
                    if(!item.equals(bpmSalxsddcWithBLOBsDb)){
                        bpmSalxsddcDao.updateByPrimaryKey(item);
                        updateCount++;
                    }
                }
            }catch (Exception e){
                exceptionCount++;
            }
        }
        Etllog etllog = new Etllog("bpm销售订单明细", insertCount, updateCount, exceptionCount, 0);
        etllogDao.insert(etllog);
        System.out.println("bpm销售订单明细" + insertCount + "条,修改" + updateCount + "条,异常处理" + exceptionCount + "条");
    }

    public void processBpmErClbxdm(){
        List<HashMap<String, Object>> res = caiWuDao.listErClbxdm();
        int insertCount = 0;
        int updateCount = 0;
        int exceptionCount = 0;
        for (HashMap<String, Object> map : res) {
            try {
                BpmErClbxdm item = new BpmErClbxdm();
                Util.mapToBean(map, item);
                BpmErClbxdm bpmErClbxdmDb = bpmErClbxdmDao.selectByPrimaryKey(item.getIid());
                if(bpmErClbxdmDb == null){
                    bpmErClbxdmDao.insert(item);
                    insertCount++;
                }else{
                    if(!item.equals(bpmErClbxdmDb)){
                        bpmErClbxdmDao.updateByPrimaryKey(item);
                        updateCount++;
                    }
                }
            }catch (Exception e){
                exceptionCount++;
            }
        }
        Etllog etllog = new Etllog("bpm差旅报销", insertCount, updateCount, exceptionCount, 0);
        etllogDao.insert(etllog);
        System.out.println("bpm差旅报销" + insertCount + "条,修改" + updateCount + "条,异常处理" + exceptionCount + "条");
    }

    public void processBpmErRcfybxdm(){
        List<HashMap<String, Object>> res = caiWuDao.listErRcfybxdm();
        int insertCount = 0;
        int updateCount = 0;
        int exceptionCount = 0;
        for (HashMap<String, Object> map : res) {
            try {
                BpmErRcfybxdm item = new BpmErRcfybxdm();
                Util.mapToBean(map, item);
                BpmErRcfybxdm bpmErRcfybxdmDb = bpmErRcfybxdmDao.selectByPrimaryKey(item.getIid());
                if(bpmErRcfybxdmDb == null){
                    bpmErRcfybxdmDao.insert(item);
                    insertCount++;
                }else{
                    if(!item.equals(bpmErRcfybxdmDb)){
                        bpmErRcfybxdmDao.updateByPrimaryKey(item);
                        updateCount++;
                    }
                }
            }catch (Exception e){
                exceptionCount++;
            }
        }
        Etllog etllog = new Etllog("bpm日常费用报销", insertCount, updateCount, exceptionCount, 0);
        etllogDao.insert(etllog);
        System.out.println("bpm日常费用报销" + insertCount + "条,修改" + updateCount + "条,异常处理" + exceptionCount + "条");
    }

    public void processBpmPurFksqdm(){
        List<HashMap<String, Object>> res = caiWuDao.listPurFksqdm();
        int insertCount = 0;
        int updateCount = 0;
        int exceptionCount = 0;
        for (HashMap<String, Object> map : res) {
            try {
                BpmPurFksqdm item = new BpmPurFksqdm();
                Util.mapToBean(map, item);
                BpmPurFksqdm bpmErRcfybxdmDb = bpmPurFksqdmDao.selectByPrimaryKey(item.getIid());
                if(bpmErRcfybxdmDb == null){
                    bpmPurFksqdmDao.insert(item);
                    insertCount++;
                }else{
                    if(!item.equals(bpmErRcfybxdmDb)){
                        bpmPurFksqdmDao.updateByPrimaryKey(item);
                        updateCount++;
                    }
                }
            }catch (Exception e){
                exceptionCount++;
            }
        }
        Etllog etllog = new Etllog("bpm费报付款申请单", insertCount, updateCount, exceptionCount, 0);
        etllogDao.insert(etllog);
        System.out.println("bpm费报付款申请单" + insertCount + "条,修改" + updateCount + "条,异常处理" + exceptionCount + "条");
    }
}
